<template>
  <div style="position: relative">
    <div class="mask" v-if="handleType=='look'"></div>
    <a-form
      ref="formRef"
      layout="vertical"
      :model="formState"
      :rules="rules"
      :wrapper-col="wrapperCol"
      style="display: flex; flex-wrap: wrap"
    >
     <a-form-item
        style="width: 50%; padding-right: 15px"
        ref="userStaffId"
        label="领取人"
        name="userStaffId"
      >
       <a-select
            allowClear
            style="width: 100%"
            v-model:value="formState.userStaffId"
            placeholder="请选择领取人"
          >
            <a-select-option
              v-for="item in userStaffList"
              :key="item.id"
              :value="item.id"
              >{{ item.realName }}</a-select-option
            >
          </a-select>
      </a-form-item>
     <a-form-item
        style="width: 50%; padding-left: 15px"
        ref="hospitalDepartId"
        label="所属科室"
        name="hospitalDepartId"
      >
        <a-tree-select
          v-model:value="formState.hospitalDepartId"
          style="width: 100%"
          :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
          :tree-data="departmentList"
          placeholder="请选择科室"
          tree-default-expand-all
        >
        </a-tree-select>
      </a-form-item>
       <a-form-item
        style="width: 50%; padding-right: 15px"
        ref="materialBaseId"
        label="耗材编码"
        name="materialBaseId"
      >
        <a-select
            allowClear
            style="width: 100%"
             @change="handleChooseMater"
            v-model:value="formState.materialBaseId"
            placeholder="请选择耗材编码"
          >
            <a-select-option
              v-for="item in componentsList"
              :key="item.id"
              :value="item.id"
              >{{ item.code }}</a-select-option
            >
          </a-select>
      </a-form-item>
      <a-form-item
        style="width: 50%; padding-left: 15px"
        ref="materialBaseName"
        label="耗材名称"
        name="materialBaseName"
      >
      <a-input disabled placeholder="请输入耗材名称" v-model:value="formState.materialBaseName" ></a-input>
        
      </a-form-item>

      <a-form-item
        style="width: 50%; padding-right: 15px"
        ref="applyNum"
        label="领用数量"
        name="applyNum"
      >
         <a-input-number style="width:100%"  v-model:value="formState.applyNum" :min="0" placeholder="请输入领用数量" />
      </a-form-item>
        
       <a-form-item
        style="width: 50%; padding-left: 15px"
        ref="applyTime"
        label="领用时间"
        name="applyTime"
      >
        <a-date-picker style="width:100%" v-model:value="formState.applyTime" />
      </a-form-item>
   
      <a-form-item
        style="width: 100%"
        ref="remark"
        label="领用说明"
        name="remark"
      >
       <a-textarea placeholder="请输入领用说明" v-model:value="formState.remark" showCount :maxlength="120" />
      </a-form-item>
    </a-form>
  </div>
</template>
<script>
import {
  defineComponent,
  onMounted,
  reactive,
  ref,
  toRaw,
  toRef,
  toRefs,
  watch,
} from "vue";
import * as inventoryApi from "@/api/inventory.js";
import * as department from "@/api/department.js";
import { message } from "ant-design-vue";
import { getUserInfo } from "@/utils/auth";
import * as consumableApi from "@/api/consumable.js";
import { hospitalDepartList } from "@/api/hospitalDepart.js";
import * as utils from "@/utils/index.js";
 

const rules = {
        //form表单非空校验
        applyNum: [
          {
            required: true,
            message: "请输入领取数量",
          },
        ],
        hospitalDepartId: [
          {
            required: true,
            message: "请选择科室",
            trigger: "change",
          },
        ],
        materialBaseId: [
          {
            required: true,
            message: "请选择耗材编号",
            trigger: "change",
          },
        ],
        userStaffId: [
          {
            required: true,
            message: "请选择领取人",
            trigger: "blur",
          },
        ],
      };
 
export default defineComponent({
  props: {
    handleType: {
      type: String,
      default: "add",
    },
    handleRow: {
      type: Object,
      default: {},
    },
  },
  setup(props, { emit }) {
    const handleType = toRef(props, "handleType");
    const formRef = ref();
    const formState = reactive({
       id: "",
        applyNum: 0,
        hospitalDepartId: null,
        materialBaseId: null,
        materialBaseName: "",
        reserve: 0,
        userStaffId: null,
        applyTime: "",
        remark: "",
    });
    const userInfo = ref(getUserInfo() ? JSON.parse(getUserInfo()) : {});

  // 获取耗材
  const componentsList = ref([])
  const handleChooseMater = (val)=>{
   console.log(val)
   formState.materialBaseName = componentsList.value.filter((item)=>{
     return item.id == val
   })[0].name
  }
   //获取耗材列表数据
    const handleGetDatacomponentsList = ()=>{
      consumableApi.materialBaseList({ isPage: false }).then((res) => {
        if (res && res.success) {
          componentsList.value = res.data;
        }
      });
    }
    // 获取领取人
    const userStaffList = ref([])
     //获取领取人列表数据
    const handleGetuserStaffList = ()=>{
      department.sysUserStaffList({ isPage: false }).then((res) => {
        if (res && res.success) {
          console.log('zre')
          console.log(res.data)
          userStaffList.value = res.data;
        }
      });
    }
   
    // 获取科室数据
    const departmentList = ref([]);
    const getKSData = async () => {
      let res = await hospitalDepartList({
        isPage: false,
        name: "",
      });
      if (res && res.success) {
        let departmentListO = res.data.map((item) => {
          item.label = item.name;
          item.value = item.id
          return item;
        });
        departmentList.value = utils.treeDataTranslate(departmentListO);
        console.log(123444444);
        console.log(departmentList.value);
      }
    };
    // 保存数据
    const handleSave = () => {
      if (props.handleType == "look") {
        resetForm();
        emit("finish");
        return false;
      }
      formRef.value.validate().then(() => {
        let params = {
          ...toRaw(formState),
        };
        inventoryApi
            .materialApplySave(params).then((res) => {
          if (res && res.success) {
            message.success("操作成功");
            resetForm();
            emit("finish");
          } else {
          }
        });
      });
    };
    const resetForm = () => {
      formRef.value.resetFields();
    };
    onMounted(async () => {
      getKSData()
      handleGetuserStaffList()
      handleGetDatacomponentsList()
      if (props.handleType == "add") {
        formState.id = "";
        return false;
      }
      console.log(2323)
      for(let self in props.handleRow){
         formState[self] = props.handleRow[self];
      }
    
    });

    return {
      formRef,
    
      // 耗材列表
      componentsList,
      // 领取人
      userStaffList,
     
      // 科室列表
      departmentList, //
      handleType,
      userInfo,
      wrapperCol: {
        span: 0,
      },
      formState,
      rules,
      handleSave,
      resetForm,

      handleChooseMater
    };
  },
});
</script>
<style lang="scss">
.mask{
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1000;
}
</style>